﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RentIt06WPFClient.UserSession;

namespace RentIt06WPFClient
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Page
    {

        private readonly Action<Account> updateWindow;

        public Login(Action<Account> updateWindow) {
            InitializeComponent();
            EmailBox.Focus();
            this.updateWindow = updateWindow;
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e) {
            var email = EmailBox.Text;
            var password = PasswordBox.Password;
            Account account;
            try {
                account = Session.Login(email, password);
            } catch (Exception exc) {
                MessageLabel.Foreground = Brushes.Red;
                MessageLabel.Content = exc.Message;
                return;
            }
            updateWindow(account);
            NavigationService.Navigate(new FrontPage());
        }

        private void CreateAccountButton_Click(object sender, RoutedEventArgs e) {
            ShowRepeatPassword();
        }
        private void AcceptCreateButton_Click(object sender, RoutedEventArgs e) {
            var password = PasswordBox.Password;
            var email = EmailBox.Text;
            if(email == "") {
                MessageLabel.Foreground = Brushes.Red;
                MessageLabel.Content = "Email field is empty!";
                return;
            } if(RepeatPasswordBox.Password != password) {
                MessageLabel.Foreground = Brushes.Red;
                MessageLabel.Content = "Passwords do not match!";
                return;
            }
            try {
                Session.CreateAccount.CreateAccount(email, password);
            } catch (FaultException exc) {
                MessageLabel.Foreground = Brushes.Red;
                MessageLabel.Content = exc.Message;
                return;
            }
            MessageLabel.Foreground = Brushes.Green;
            MessageLabel.Content = "Account succesfully created, you can now login!";
            HideRepeatPassword();
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e) {
            HideRepeatPassword();
        }
        private void RepeatPasswordBox_PasswordChanged(object sender, RoutedEventArgs e) {
            if (RepeatPasswordBox.Password == PasswordBox.Password) {
                PasswordsMatchingImg.Visibility = Visibility.Visible;
                PasswordsNotMatchingImg.Visibility = Visibility.Hidden;
            } else {
                PasswordsNotMatchingImg.Visibility = Visibility.Visible;
                PasswordsMatchingImg.Visibility = Visibility.Hidden;
            }
        }

        private void ShowRepeatPassword() {
            CreateAccountButton.Visibility = Visibility.Hidden;
            LoginButton.Visibility = Visibility.Hidden;
            MessageLabel.Margin = new Thickness(29,220,0,0);

            RepeatPasswordLabel.Visibility = Visibility.Visible;
            RepeatPasswordBox.Visibility = Visibility.Visible;
            AcceptCreateButton.Visibility = Visibility.Visible;
            CancelButton.Visibility = Visibility.Visible;
        }
        private void HideRepeatPassword() {
            CreateAccountButton.Visibility = Visibility.Visible;
            LoginButton.Visibility = Visibility.Visible;
            MessageLabel.Margin = new Thickness(29,147,0,0);

            RepeatPasswordLabel.Visibility = Visibility.Hidden;
            RepeatPasswordBox.Visibility = Visibility.Hidden;
            RepeatPasswordBox.Password = "";
            PasswordsMatchingImg.Visibility = Visibility.Hidden;
            PasswordsNotMatchingImg.Visibility = Visibility.Hidden;
            AcceptCreateButton.Visibility = Visibility.Hidden;
            CancelButton.Visibility = Visibility.Hidden;
        }
    }
}
